﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_BasicInfo_User : System.Web.UI.UserControl
{
    security baomat = new security();
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        using (Up1VNDataContext dc = new Up1VNDataContext())
        {
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    hndusername.Value = _user["username"];
                    var us = from o in dc.Users
                                where o.Users_Name == _user["username"]
                                select o;
                    if (us.Count() > 0)
                    {
                        loadinfo(_user["username"]);
                        showInfo();
                    }
                    else
                    {
                        hideInfo();
                    }
                }
            }
            else
            {
                if (Page.RouteData.Values.Count > 0)
                    if (Page.RouteData.GetRequiredString("User") != "" || Page.RouteData.GetRequiredString("User") != null)
                    {
                        string username = Page.RouteData.GetRequiredString("User");
                        loadinfo(username);
                        hideInfo();
                    }
                    else
                        Response.RedirectToRoute("Index");
                else
                    Response.RedirectToRoute("Index");
            }
            dc.Dispose();
        }
    }
    private void hideInfo()
    {
        //divCurrentPoint.Visible = false;
        divEditInfo.Visible = false;
        divPWD.Visible = false;
        //divRank.Visible = false;
        divUsername.Visible = false;
        EditEmail.Visible = false;
        EditHomePhone.Visible = false;
        EditMobile.Visible = false;
        EditYahoo.Visible = false;
        EditSkype.Visible = false;
    }
    private void showInfo()
    {
        //divCurrentPoint.Visible = true;
        divEditInfo.Visible = true;
        divPWD.Visible = true;
        //divRank.Visible = true;
        divUsername.Visible = true;
        EditEmail.Visible = true;
        EditHomePhone.Visible = true;
        EditMobile.Visible = true;
        EditYahoo.Visible = true;
        EditSkype.Visible = true;
    }
    private void loadinfo(string username)
    {
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var user = dc.Users.Single(o => o.Users_Name == username);
                lblusername.Text = username;
                txtEmail.Text = user.Users_Email;
                lblGold.Text = user.Users_Point.ToString();
                lblfullname.Text = user.Users_FullName;
                lblbirthday.Text = user.Users_BirdthDay.ToString() != "" ? baomat.convertTimeTo_dMy(user.Users_BirdthDay.ToString()) : "";
                lblsex.Text = user.Users_Sex.ToString();
                imgAvatar.ImageUrl = (user.Users_Avatar != "") ? ResolveUrl("~/User/avatar/" + user.Users_Avatar) : ResolveUrl("~/images/noavatar.png");
                lbladdress.Text = user.Users_Address;
                txtMobile.Text = (user.Users_MobilePhone != null) ? user.Users_MobilePhone : "Chưa cập nhật";
                txthomephone.Text = (user.Users_HomePhone != null) ? user.Users_HomePhone : "Chưa cập nhật";
                txtyahoo.Text = (user.Users_YahooNick != "") ? user.Users_YahooNick : "Chưa cập nhật";
                txtskype.Text = (user.Users_SkypeNick != "") ? user.Users_SkypeNick : "Chưa cập nhật";
            }
        }
        catch (Exception)
        {

        }
    }
}